﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebService_Newstore.DTO;
using WebService_Newstore.DAO;
using System.Globalization;

namespace WebService_Newstore.Controller
{
    public class ServiceBPController
    {
        /// <summary>
        /// Méthode permettant de renvoyer toutes les commandes à expédier à la date choisie, la gestion des jours "ouvrés" se fera dans le client
        /// </summary>
        /// <param name="dateExp">date du prochain jour ouvré, mettre l'heure à 0 </param>
        /// <returns>la liste des commandes dont le jour de livraison et le jour choisit</returns>
        public static List<CommandeDTO> CommandeToSend(DateTime dateExp)
        {
            List<CommandeDTO> listeCmdExp = new List<CommandeDTO>();
            List<CommandeDAO> listeCmd = new List<CommandeDAO>();

            listeCmd=CommandeDAO.getCommandeDAOByDate(dateExp);

            //Si on ne récupère aucune commande, on renvoie une liste vide
            if (listeCmd == null)
            {
                return listeCmdExp;
            }
            else
            {
                //On instancie les commandes DTO et on les ajoutes à la liste
                foreach (CommandeDAO cmd in listeCmd)
                {
                    CommandeDTO cmdToAdd = new CommandeDTO();
                    cmdToAdd.idCommande = cmd.idCommande;
                    cmdToAdd.dateCmd = cmd.dateCmd;
                    cmdToAdd.dateLivrCmd = cmd.dateLivrCmd;
                    cmdToAdd.refCommande = cmd.refCommande;
                    cmdToAdd.statutCmd = cmd.statutCmd;
                    cmdToAdd.idHyper = cmd.idHyper;
                    cmdToAdd.iduser = cmd.idUser;
                    listeCmdExp.Add(cmdToAdd);
                }

                return listeCmdExp;
            }
        }

        public static List<LgCmdDTO> GetLgCmdByIDCmd(int idCmd)
        {
            List<LgCmdDTO> List_LgCmdDTO = new List<LgCmdDTO>();
            List<LgCmdDAO> List_LgCmdDAO = new List<LgCmdDAO>();

            List_LgCmdDAO = LgCmdDAO.getLgCmdDAOByCmd(idCmd);

            if (List_LgCmdDAO == null)
            {
                return List_LgCmdDTO;
            }
            else
            {
                foreach (LgCmdDAO lgDAO in List_LgCmdDAO)
                {
                    LgCmdDTO lgCmdToAdd = new LgCmdDTO();
                    lgCmdToAdd.idLgCmd = lgDAO.id;
                    lgCmdToAdd.code_uniteCmd = lgDAO.code_uniteCmd;
                    lgCmdToAdd.idArticle = lgDAO.idArticle;
                    lgCmdToAdd.numCmd = lgDAO.numeroCommande;
                    lgCmdToAdd.numLigne = lgDAO.numeroLigne;
                    lgCmdToAdd.qteCmdee = lgDAO.QteCommandee;
                    lgCmdToAdd.qteLivree = lgDAO.QteLivree;
                    lgCmdToAdd.qteRef = lgDAO.QteReference;
                    lgCmdToAdd.statut = lgDAO.statut;
                    List_LgCmdDTO.Add(lgCmdToAdd);
                }

                return List_LgCmdDTO;
            }
        }

        public static List<EmplacementDTO> findEmplacementByArticle(int idArticle,string codeUnite)
        {
            List<EmplacementDAO> listEmpDAO = EmplacementDAO.getEmplacementByArticleID(idArticle,codeUnite);
            List<EmplacementDTO> listeEmpDTO = new List<EmplacementDTO>();

            

            if(listEmpDAO.Count()!=0)
            {
                foreach (EmplacementDAO empDAO in listEmpDAO)
                {
                    EmplacementDTO empDTO = new EmplacementDTO();
                    empDTO.idEmplacement = empDAO.id;
                    empDTO.libEmplacement = empDAO.lib;
                    empDTO.isPicking_Emplacement = empDAO.isPicking;
                    empDTO.stockAlerte_Emplacement = empDAO.stockAlerte;
                    empDTO.stockMax_Emplacement = empDAO.stockMax;
                    empDTO.stockQte_Emplacement = empDAO.stockQte;
                    empDTO.idArticle_Emplacement = empDAO.idArticle;
                    empDTO.idUnite_Emplacement = empDAO.idUnite;
                    listeEmpDTO.Add(empDTO);
                }
                return listeEmpDTO;
            }
            else
            {
                //Sinon on retourne une liste d'emplacement vide
                return listeEmpDTO;
            }
        }

        private static List<EmplacementDAO> getEmplacementByArticleID(int idArticle)
        {
            throw new NotImplementedException();
        }

        public static void SaveCmdDTO(CommandeDTO cmdDTO)
        {
            CommandeDAO.UpdateCmd(new CommandeDAO(cmdDTO.idCommande,cmdDTO.dateCmd,cmdDTO.dateLivrCmd,cmdDTO.refCommande,cmdDTO.idHyper,cmdDTO.statutCmd,cmdDTO.iduser));
        }

        public static List<BPDTO> findBPByIdCmd(int idCmd)
        {
            List<BPDTO> ListToSend = new List<BPDTO>();
            List<BpDAO> ListBPDAO = BpDAO.FindBpDAOByCmd(idCmd);

            if (ListBPDAO.Count() > 0)
            {
                foreach (BpDAO bpDAO in ListBPDAO)
                {
                    BPDTO bpDToAdd = new BPDTO(bpDAO.numero, bpDAO.date, bpDAO.statut, bpDAO.numeroCommande, bpDAO.type, bpDAO.idUser);
                    ListToSend.Add(bpDToAdd);
                }
                return ListToSend;
            }
            else
            {
                //On retourne une liste vide
                return ListToSend;
            }
        }

        public static BPDTO SaveBPDTO(BPDTO bpDTO)
        {
             BpDAO bpdao = BpDAO.SaveBP(new BpDAO(bpDTO.dateBP, bpDTO.statutBP, bpDTO.idCmd, bpDTO.typeBP,bpDTO.idUser));
             return new BPDTO(bpdao.numero, bpdao.date, bpdao.statut, bpdao.numeroCommande, bpdao.type, bpdao.idUser);
        }

        public static LgBpDTO SaveLgBpDTO(LgBpDTO lgbpdto)
        {
            LgBpDAO lgbpdao = LgBpDAO.Update(new LgBpDAO(lgbpdto.id,lgbpdto.idBp,lgbpdto.numeroLigne,lgbpdto.qteAPrepa,lgbpdto.qtePreparee,lgbpdto.statut,lgbpdto.codeUnite,
                lgbpdto.qteReference,lgbpdto.idEmplacement,lgbpdto.idAnomalie, lgbpdto.idLgCmd));
            //Mise à jour de l'id
            lgbpdto.id = lgbpdao.id;
            return lgbpdto;
        }

        public static void SaveEmplacementDTO(EmplacementDTO empDTO)
        {
            EmplacementDAO.SaveEmplacementDAO(new EmplacementDAO(empDTO.idEmplacement,empDTO.libEmplacement,empDTO.isPicking_Emplacement,empDTO.stockMax_Emplacement,empDTO.stockAlerte_Emplacement,empDTO.stockQte_Emplacement,empDTO.idUnite_Emplacement,empDTO.idArticle_Emplacement));
        }

        public static void SaveLgCmd(LgCmdDTO lgcmddto)
        {
            LgCmdDAO.SaveLgCmd(new LgCmdDAO(lgcmddto.idLgCmd, lgcmddto.numCmd, lgcmddto.numLigne, lgcmddto.idArticle, lgcmddto.qteCmdee, lgcmddto.qteLivree, lgcmddto.statut, lgcmddto.code_uniteCmd, lgcmddto.qteRef));
        }

        public static float GetStThEmp(int idEmp)
        {
            return LgBpDAO.getStockTheoric(idEmp);
        }

        public static List<LgBpDTO> GetLgBPByBP(int idbp)
        {
            List<LgBpDAO> ListeDAO = LgBpDAO.FindLgByBp(idbp);
            List<LgBpDTO> ListeDTO = new List<LgBpDTO>();

            foreach(LgBpDAO lgDAO in ListeDAO)
            {
                ListeDTO.Add(new LgBpDTO(lgDAO.id, lgDAO.idBp, lgDAO.numeroLigne, lgDAO.idLgCmd, lgDAO.qteAPrepa, lgDAO.qtePreparee, lgDAO.statut, lgDAO.idEmplacement, lgDAO.codeUnitePrepa, lgDAO.idAnomalie));
            }

            return ListeDTO;
        }

        public static EmplacementDTO GetEmplacementByID(int idEmp)
        {
            EmplacementDAO empDAO = EmplacementDAO.getEmplacementByID(idEmp);
            return new EmplacementDTO(empDAO.id, empDAO.lib, empDAO.isPicking, empDAO.stockMax, empDAO.stockAlerte, empDAO.stockQte, empDAO.idUnite, empDAO.idArticle);
        }

        public static List<BPDTO> findBPByDate(DateTime start, DateTime end)
        {
            List<BPDTO> ListToSend = new List<BPDTO>();
            List<BpDAO> ListBPDAO = BpDAO.FindBPByDate(start, end);

            if (ListBPDAO.Count() > 0)
            {
                foreach (BpDAO bpDAO in ListBPDAO)
                {
                    BPDTO bpDToAdd = new BPDTO(bpDAO.numero, bpDAO.date, bpDAO.statut, bpDAO.numeroCommande, bpDAO.type, bpDAO.idUser);
                    ListToSend.Add(bpDToAdd);
                }
                return ListToSend;
            }
            else
            {
                //On retourne une liste vide
                return ListToSend;
            }
        }

    }
}
